﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Entity.Validation;
using HYI.SDBSERVER;
using eHCBaseClass;
using Sunny.UI;

namespace SHRSTOCK
{
    public partial class FMS_TS_EQUIPMENT_DICT : SDBSERVER.FMSINGLE
    {
        //入口函数
        public static Int32 StartMe(string sMainPara, string sPara, string sUser, DBSERVER sDB)
        {
            StaticClass.Selectm_MainPara = sMainPara;
            StaticClass.SelectItemValue = sPara;
            StaticClass.SelectUser = sUser;
            StaticClass.myDB = sDB;
            return 0;
        }

        private int iGridView = 0;
        private string sType = "V";
        private string sSite_No = string.Empty;
        //DBSERVER myADO = new DBSERVER();
        DBCache myCache = new DBCache();

        public FMS_TS_EQUIPMENT_DICT()
        {
            InitializeComponent();
        }

        //查询代码
        protected override void OnSys04()
        {
            string SQL = " select * from S_TS_EQUIPMENT_DICT where 1=1 ";
            Qrytable = StaticClass.myDB.query(SQL);

            base.OnSys04();

            //表格列显示统一控制
            if (iGridView == 0)
            {
                myCache.doSetGridVIew(sMenu, sUser, uiDGView);
                iGridView = iGridView + 1;
            }
        }

        private void FMS_TS_EQUIPMENT_DICT_Load(object sender, EventArgs e)
        {
            sUser = StaticClass.SelectUser;
            string[] strArray = StaticClass.Selectm_MainPara.Split(new char[] { ';' });
            sMenu = strArray[1];
            sSite_No = strArray[0];

            //按钮控制
            sys01.Visible = false; sys01.Tag = "1";
            sys03.Visible = false; sys03.Tag = "1";
            sys05.Visible = false; sys05.Tag = "1";
            sys06.Visible = false; sys06.Tag = "1";
            sys07.Visible = false; sys07.Tag = "1";
            sys08.Visible = false; sys08.Tag = "1";
            sys09.Visible = false; sys09.Tag = "1";
            sys11.Visible = false; sys11.Tag = "1";

            //执行查询代码
            OnSys04();
        }

        //新增代码
        protected override void OnSys01()
        {
            uifdictno.Text = "";
            uifdictname.Text = "";

            sType = "A";
            doLoadData();
            uifdictno.ReadOnly = false;

            base.OnSys01();
        }

        //编辑+浏览载入数据
        private void doLoadData()
        {
            if (!sType.Equals("A"))
            {
                uifdictno.Text = uiDGView["fdictno", rowindex].Value.ToString();
                uifdictname.Text = uiDGView["fdictname", rowindex].Value.ToString();
            }

        }

        //编辑代码
        protected override void OnSys02()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }
            sType = "B";

            doLoadData();

            uifdictno.ReadOnly = true;
            uifdictname.ReadOnly = false;

            base.OnSys02();
        }

        //浏览代码
        protected override void OnSys10()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }
            sType = "V";

            doLoadData();
            uifdictno.ReadOnly = true;
            uifdictname.ReadOnly = true;

            base.OnSys10();
        }

        //数据为空检查
        private bool CheckData()
        {
            return CheckEmpty(uifdictno, "请输入类型编号")
                   && CheckEmpty(uifdictname, "请输入类型名称");
        }

        //保存代码
        protected override void OnSys08()
        {
            if (!CheckData())
            {
                return;
            }
            
            if (sType == "A")
            {
                try
                {
                    using (XTBasEntities context = new XTBasEntities())
                    {
                        s_ts_equipment_dict myData = context.s_ts_equipment_dict.Find(int.Parse(uifdictno.Text));
                        if (myData is null)
                        {
                            s_ts_equipment_dict myData2 = new s_ts_equipment_dict();
                            myData2.fdictno =int.Parse(uifdictno.Text);
                            myData2.fdictname = uifdictname.Text;
                            myData2.foperator = sUser;
                            myData2.fupddate = DateTime.Now;
                            context.s_ts_equipment_dict.Add(myData2);
                            context.SaveChanges();

                            UIMessageTip.ShowOk("添加成功");

                        }
                        else
                        {
                            //Console.WriteLine("添加成功");
                            UIMessageTip.ShowOk("编号已存在");
                            //Console.WriteLine("已存在");
                            return;
                        }

                    }
                }
                catch (DbEntityValidationException dbEx)
                {
                    Console.WriteLine("错误" + dbEx.Message);
                }
            }

            else if (sType == "B")
            {
                using (XTBasEntities context = new XTBasEntities())
                {
                    //查询
                    s_ts_equipment_dict myts_warehouse = context.s_ts_equipment_dict.Find(int.Parse(uifdictno.Text));

                    myts_warehouse.fdictname = uifdictname.Text;
                    myts_warehouse.foperator = sUser;
                    myts_warehouse.fupddate = DateTime.Now;
                    int result = context.SaveChanges();
                    UIMessageTip.ShowOk("修改成功");
                }
            }

            //刷新cache
            sType = "V";
            base.OnSys08();

            OnSys04();
        }

    }
}
